function OpenTabPage() {
	if (this.query.indexOf('%') == -1) {
		this.query = encodeURIComponent(this.query);
	}
	
	chrome.tabs.create({"url": "src/html/tab.html?query=" + this.query, "selected": true});
};

function TagURL(text) {
	var start = text.indexOf('http://');
	var end = text.indexOf(' ', start);
	var result = "";
	while (start != -1) {
		result += text.substring(0, start);
		if (end == -1) {
			end = text.length;
		}
		result += "<a href='" + text.substring(start, end) + "' target='_blank'>" + text.substring(start, end) + "</a>";
		
		text = text.substring(end);
		start = text.indexOf('http://');
		end = text.indexOf(' ', start);
	}
	
	result += text;
	
	text = result;
	
	start = text.indexOf('https://');
	end = text.indexOf(' ', start);
	result = "";
	while (start != -1) {
		result += text.substring(0, start);
		if (end == -1) {
			end = text.length;
		}
		result += "<a href='" + text.substring(start, end) + "' target='_blank'>" + text.substring(start, end) + "</a>";
		
		text = text.substring(end);
		start = text.indexOf('https://');
		end = text.indexOf(' ', start);
	}
	
	result += text;
	
	return result;
};

function Location(id, name) {
	this.id = id;
	this.name = name;
};

function Trend(name, query, url, date) {
	// Properties
	this.weight = 0;
	this.name = name;
	this.query = query;
	this.url = url;
	this.date = date;
	
	// Methods
	this.AddWeight = function(value) {
		this.weight += value;
	};
};

function Parser() {
	// Properties
	this.last_updated = 0;
	
	// Methods
	this.Parser = function(data) {};
	this.Printer = function(dom) {};
	
	this.Parse = function(data) {
		this.last_updated = new Date();
		this.Parser(data);
	};
	
	this.Print = function(dom) {
		var para = document.createElement("p");
		var text = document.createTextNode("Last updated: " + this.last_updated.toShortString());
		para.appendChild(text);
		dom.appendChild(para);
		
		this.Printer(dom);
	};
};

function DateParser() {
	// Inheritance 
	Parser.apply(this);
	
	// Properties
	this.trends = new Array();
	this.created_at = "";
	
	// Methods
	function SortTrends(a, b) {
		return a.weight - b.weight;
	};

	this.Parser = function(data) {
		this.trends = new Array();
		var objList = new Object();
		var trendslist = data.query.results.trends;
		this.created_at = data.query.created;
		if (!trendslist) {
			return;
		}
		for (var i in trendslist) {
			for (var j = 0; j < trendslist[i].length; j++) {
				if (!objList[trendslist[i][j].name]) {
					objList[trendslist[i][j].name] = new Trend(trendslist[i][j].name, trendslist[i][j].query, trendslist[i][j].url, new Date());
				}
				objList[trendslist[i][j].name].AddWeight(j);
			}
		}
		
		for (var i in objList) {
			this.trends.push(objList[i]);
		}
		
		this.trends = this.trends.sort(SortTrends);
	};
	
	this.Printer = function(dom) {
		var newList = document.createElement("ol");
		dom.appendChild(newList);
		
		for (var i = 0; i < this.trends.length && i < 20; i++) {	
			var newListItem = document.createElement("li");
			newList.appendChild(newListItem);
			var anchorItem = document.createElement("a");
			anchorItem.setAttribute('href', '#');
			newListItem.appendChild(anchorItem);
			anchorItem.onclick = OpenTabPage;
			//anchorItem.onclick = function() {chrome.tabs.create({"url": "src/html/tab.html?query=" + this.query, "selected": true})};
			anchorItem.innerText = this.trends[i].name;
			anchorItem.query = this.trends[i].query;
		}
	}
};

function LocationParser() {
	// Inheritance 
	Parser.apply(this);
	
	// Properties
	this.trends = new Array();
	this.location = new Location();
	this.as_of = "";
	this.created_at = "";
	
	// Methods
	this.Parser = function(data) {
		this.trends = new Array();
		var result = JSON.parse(data.query.results.result);
		if (!result) {
			return;
		}
		var trendslist = result[0].trends;
		this.as_of = result[0].as_of;
		this.created_at = result[0].created_at;
		this.location.id = result[0].locations[0].woeid;
		this.location.name = result[0].locations[0].name;
		if (!trendslist) {
			return;
		}
		for (var i = 0; i < trendslist.length; i++) {
			this.trends.push(new Trend(trendslist[i].name, trendslist[i].query, trendslist[i].url, new Date()));
		}
	};
	
	this.Printer = function(dom) {
		var para = document.createElement("p");
		var text = document.createTextNode("Location: " + this.location.name);
		para.appendChild(text);
		dom.appendChild(para);
		
		var newList = document.createElement("ol");
		dom.appendChild(newList);
		
		for (var i = 0; i < this.trends.length; i++) {
			var newListItem = document.createElement("li");
			newList.appendChild(newListItem);
			var anchorItem = document.createElement("a");
			anchorItem.setAttribute('href', '#');
			newListItem.appendChild(anchorItem);
			anchorItem.onclick = OpenTabPage;
			//anchorItem.onclick = function() {chrome.tabs.create({"url": "src/html/tab.html?query=" + this.query, "selected": true})};
			anchorItem.innerText = this.trends[i].name;
			anchorItem.query = this.trends[i].query;
		}
	}
};

function SearchParser() {
	// Inheritance 
	Parser.apply(this);
	var _results = null;
	
	// Methods
	this.Parser = function(data) {
		_results = data.query.results.results;
	};
	
	this.Printer = function(dom) {
		if (_results) {
			for (var i = 0; i < _results.length; i++ ) {
				var tweetblock = document.createElement("div");
				tweetblock.className = "tweet_block";
				var nameblock = document.createElement("div");
				nameblock.className = "tweet_name_block";
				nameblock.innerHTML = _results[i].from_user_name;
				var imgblock = document.createElement("div");
				imgblock.className = "tweet_profile_img_block";
				imgblock.innerHTML = "<img src='" + _results[i].profile_image_url + "'/>";
				var textblock = document.createElement("div");
				textblock.className = "tweet_text_block";
				textblock.innerHTML = TagURL(_results[i].text);
				tweetblock.appendChild(nameblock);
				tweetblock.appendChild(imgblock);
				tweetblock.appendChild(textblock);
				dom.appendChild(tweetblock);
			}
		}
	};
};

function CredentialParser() {
	// Inheritance 
	Parser.apply(this);
	
	// Methods
	this.Parser = function(data) {
		var cred = data.query.results.user;
		
		if (cred.default_profile_image == "false") {
			user.SetProfile(cred.profile_image_url);
		}
	};
};

function TrendsAvailable() {
	// Inheritance 
	Parser.apply(this);
	
	// Methods
	this.Parser = function(data) {
		var locations = new Array();
		var resp = JSON.parse(data.query.results.result);
		if (resp == null) {
			return;
		}

		for (var i = 0; i < resp.length; i++) {
			if (resp[i].placeType.code == 12) {
				locations.push(new Location(resp[i].woeid, resp[i].name));
			}
		}
		
		options.SetLocations(locations);
	};
};